package math;

public class LogicalArithmeticVariable implements ArithmeticExpression {

  private String name;

  private ArithmeticExpression value;

  public LogicalArithmeticVariable(String name) {
    this.name = name;
  }

  public void bind(ArithmeticExpression value) {
    this.value = value;
  }

  @Override
  public int countMembers() {
    return 1;
  }

  @Override
  public ArithmeticExpression derivate(Variable var) {
    throw new RuntimeException("LogicalVariables can not be derivated");
  }

  public String getName() {
    return name;
  }

  @Override
  public int getPriority() {
    return 1;
  }

  public ArithmeticExpression getValue() {
    return value;
  }

  public boolean isBound() {
    return value != null;
  }

  @Override
  public boolean isOne() {
    return false;
  }

  @Override
  public boolean isZero() {
    return false;
  }

  @Override
  public boolean matches(Expression expr) {
    if (isBound()) {
      return value.matches(expr);
    } else {
      if (expr instanceof ArithmeticExpression) {
        bind((ArithmeticExpression) expr);
        return true;
      } else
        return false;
    }
  }

  public void reset() {
    value = null;
  }

  @Override
  public ArithmeticExpression simplify() {
    return this;
  }

  @Override
  public String toString() {
    return "@" + name + "=" + value;
  }

  @Override
  public <R> R accept(ExpressionVisitor<R> visitor) {
    return visitor.visitLogicalArithmeticVariable(this);
  }
}